import {Component, Output} from '@angular/core';
import {IonicPage, ModalController, NavController, NavParams} from 'ionic-angular';
import {ValidateProvider} from "../../providers/validate/validate";
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {UtilsProvider} from "../../providers/utils/utils";
import {ConfigProvider} from "../../providers/config/config";
import {CommonProvider} from "../../providers/common/common";
import {StorageProvider} from "../../providers/storage/storage";
import {WechatProvider} from "../../providers/wechat/wechat";

@IonicPage({
    segment:'service/bbs',
    defaultHistory:['HomePage','ServicePage']
})
@Component({
    selector: 'page-service-bbs',
    templateUrl: 'service-bbs.html',
})
export class ServiceBbsPage {
    @Output('reply') reply;
    //BBS论坛ID
    typeId:number = 20;
    banners:any;
    notices:any;
    page:number = 1;
    infolist = [];
    //评论框 默认隐藏
    commentData = {};
    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public validate: ValidateProvider,
        public httpService: HttpServicesProvider,
        public util: UtilsProvider,
        public config: ConfigProvider,
        public common: CommonProvider,
        public modalCtrl: ModalController,
        public storage: StorageProvider,
        public wechat: WechatProvider
    ) {
    }

    ionViewCanEnter(){
        return this.validate.checkLogin();
    }

    ionViewDidEnter(){
        this.wechat.getWxConfig(()=>{
            let userInfo = JSON.parse(this.storage.get('userInfo'));
            this.wechat.wxShareMoments({shareNumber:5, shareTitle:"请你来评价（餐饮人的生活论坛）", amoyId: 0, userId:userInfo.user_id});
            this.wechat.wxShareFriend({shareNumber:5, shareTitle:"请你来评价（餐饮人的生活论坛）", amoyId: 0, userId:userInfo.user_id});
        });

        this.page = 1;
        this.getData(null);
    }

    getData(infiniteScroll) {
        //Banner and notices
        this.httpService.requestPostData('/service/get_ads', {
            type_id: this.typeId,
            token: this.config.getToken()
        },response => {
            //获取Banner和滚动文字信息
            let data = response['data'];
            this.banners = data['image_list'];
            this.notices = data['text_list'];
        });

        // list data
        this.httpService.requestPostData('/service/get_bbs_list', {
            page: this.page,
            token: this.config.getToken()
        },response => {
            let data = response['data'];
            this.infolist = this.infolist.concat(data['info_list']['lists']);
            this.page++;
            if (infiniteScroll) {
                infiniteScroll.complete();
                /*请求完成数据更新页面*/
                if (data['info_list']['lists'].length < data['info_list']['page_size']) {   /*没有数据 禁用上拉更新*/
                    infiniteScroll.enable(false);
                }
            } else {
                this.infolist = data['info_list']['lists'];
            }
        });
    }

    doInfinite(infiniteScroll) {
        this.getData(infiniteScroll);
    }

    /**
     * 内容点赞
     * */
    like(index,amoy_id){
        this.common.like(amoy_id,(response)=>{
            if(response.code == 200){
                this.infolist[index]['has_zan'] = response['message'] == '点赞成功';
                this.getZanList(index,amoy_id);
            }else{
                this.util.presentAlert('系统有误，请稍后再试！');
            }
        });
    }

    /**
     * 获取点赞列表
     * */
    getZanList(index, amoy_id){
        let apiUrl = "/collect/zan_list";
        this.httpService.requestPostData(apiUrl,{
            token: this.config.getToken(),
            amoy_id: amoy_id
        },(response)=>{
            this.infolist[index]['zan'] = response['data'];
        })
    }

    /**
     * 留言成功后获取数据
     * */
    sendMsgSuccess(data){
        let apiUrl = "/comment/get_comment_list";
        this.httpService.requestPostData(apiUrl,{
            token: this.config.getToken(),
            amoy_id: data['amoy_id']
        },(response)=>{
            this.infolist[data['index']]['comment'] = response['data'];
        })
    }




}
